/*
code for vdrive commented out in this implementation
*/

#include "Reader.h"
#include "vdrive.h"

// pin configuration to be assigned 
int pinCLK = VdrivePinCLK;    //arbitrary values
int pinCS = VdrivePinCS;
int pinDI = VdrivePinDI;
int pinDO = VdrivePinD0;


vdrive vdr(pinCLK,pinCS,pinDI,pinDO);
extern vdrive vdr;

/**
 * @brief setup parametes for output pins for pulsing
 */
void setup()
{
  //delay in start to help debugging, remove in final code
  delay(5000);
  //set pinmodes for various IO pins  
  pinMode(STEPPinX,OUTPUT);
  pinMode(STEPPinY,OUTPUT);
  pinMode(STEPPinZ,OUTPUT);
  pinMode(STEPPinA,OUTPUT);
  pinMode(STEPPinB,OUTPUT);
  pinMode(STEPPinC,OUTPUT);
  
  pinMode(DIRPinX,OUTPUT);
  pinMode(DIRPinY,OUTPUT);
  pinMode(DIRPinZ,OUTPUT);
  pinMode(DIRPinA,OUTPUT);
  pinMode(DIRPinB,OUTPUT);
  pinMode(DIRPinC,OUTPUT);  
  
  pinMode(Spindle1,OUTPUT);
  pinMode(Spindle2,OUTPUT);
  pinMode(SpindleSpeedPin,PWM);
  pinMode(FloodPin,OUTPUT);
  pinMode(MistPin,OUTPUT);
  
  pinMode(13,OUTPUT);
}


/**
 * @brief endless loop at the end of processing
 */
void toggleForever()
{
  while(1)
  {
    digitalWrite(13,1);
    delay(50);
    digitalWrite(13,0);
    delay(50);
  }
}


/**
 * @brief starting point for the controller program execution
 */
void loop()
{  
  //intialise fifo
  // SerialUSB.end();
  digitalWrite(13,1);
  vdr.init();
  fifo_init(fifoSize);  
  //initialise pulsing for random rate
  pulseInit(1000,pulseWidth);
  
  //test code for testing sync feed interrputs
  /*
  digitalWrite(13,1);
  start_sync_timer();
  while(1)
  {
    if(SerialUSB.available())
      break;
    if(digitalRead(BOARD_BUTTON_PIN))
    {
      delay(50);//debounce delay
      handler_spindle_interrupt();
    }
    while(digitalRead(BOARD_BUTTON_PIN))
    ;
  }
  stop_sync_timer();
  */
  
  SerialUSB.println("fread called here");
  vdr.fread("out.txt");
//  reader("Out.txt");
  while(!fifo_isempty())
  ;
  //delay before pausing the pulse to give time for last pulse to show
  delay(1000);
  pausePulsing();  
  vdr.fclose("out.txt");
  toggleForever();  //to avoid iteratons of loop()  
}
